package ro.ubbcluj.cs.damate.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Timestamp;

/**
 * @author Lóránd Fazakas (lfazakas)
 *         Date: 2012.05.03.
 *         Time: 11:07
 */
public class EntityUtils {

    public static String generateUUID(String... parameters) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("SHA-256");
        } catch (NoSuchAlgorithmException e) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        for (String param : parameters) {
            sb.append(param);
        }
        md.update(sb.toString().getBytes());

        byte byteData[] = md.digest();

        StringBuffer hexString = new StringBuffer();
    	for (int i=0;i<byteData.length;i++) {
    		String hex=Integer.toHexString(0xff & byteData[i]);
   	     	if(hex.length()==1) hexString.append('0');
   	     	hexString.append(hex);
    	}
    	return hexString.toString();
    }

    public static String getDataTimeWithoutNanos(Timestamp timestamp) {
        return timestamp != null ? timestamp.toString().substring(0, timestamp.toString().indexOf('.')) : null;
    }
}
